package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.PaymentMethod;
import com.example.buildtable.service.PaymentMethodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "支付方式")
@RequestMapping(value = "/paymentMethod/")
public class PaymentMethodController {
    @Autowired
    private PaymentMethodService paymentMethodService;
    
    @Operation(summary = "查询所有支付方式数据")
    @GetMapping(value = "/getAllPaymentMethod")
    public R getAllPaymentMethod() {
        List<PaymentMethod> paymentMethodList = paymentMethodService.list();
        return R.ok("查询所有支付方式数据成功").data("paymentMethodList", paymentMethodList);
    }
    
    @Operation(summary = "增加支付方式")
    @PostMapping(value = "/addPaymentMethod")
    public R addPaymentMethod(PaymentMethod paymentMethod) {
        boolean flag = paymentMethodService.save(paymentMethod);
        if (flag) {
            return R.ok("增加支付方式成功");
        } else {
            return R.ok("增加支付方式失败");
        }
    }

    @Operation(summary = "删除支付方式")
    @GetMapping(value = "/deletePaymentMethod/{paymentMethodId}")
    public R deletePaymentMethod(@PathVariable("paymentMethodId") Integer paymentMethodId) {
        boolean flag = paymentMethodService.removeById(paymentMethodId);
        if (flag) {
            return R.ok("删除支付方式成功");
        } else {
            return R.ok("删除支付方式失败");
        }
    }

    @Operation(summary = "按照支付方式查看")
    @GetMapping(value = "/getPaymentMethodById/{paymentMethodId}")
    public R getPaymentMethodById(@PathVariable("paymentMethodId") Integer paymentMethodId) {
        PaymentMethod paymentMethod = paymentMethodService.getById(paymentMethodId);
        if (paymentMethod != null) {
            return R.ok("按照支付方式查看成功").data("paymentMethod", paymentMethod);
        } else {
            return R.error("按照支付方式查看失败");
        }
    }

    @Operation(summary = "修改支付方式")
    @PostMapping(value = "/updatePaymentMethod")
    public R updatePaymentMethod(PaymentMethod paymentMethod) {
        boolean flag = paymentMethodService.updateById(paymentMethod);
        if (flag) {
            return R.ok("修改支付方式成功");
        } else {
            return R.ok("修改支付方式失败");
        }
    }
}